// pages/publish/publish.js
const API = require('../../common/constant.js');
const util = require('../../utils/util');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    oldInfo: null,
    eventInfo: {},
    typePick: {
      array: ['#求邂逅#', '#美食搭伙#', '#解闷聊天#'],
      index: '',
      isTypePick: false,
    },
    numPick: {
      array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      index: ''
    },
    sexPick: {
      array: ['不限', '男', '女'],
      index: ''
    },
    payPick: {
      array: ['我请客', 'AA制', '男A女免'],
      index: ''
    },
    travelPick: {
      array: ['各自出行', '我接送（发起人接送）'],
      index: ''
    },
    eventAddress: "",
    eventTitle: '',
    eventContent: ''
  },
  typePickerChange: function (e) {
    this.setData({
      ['typePick.index']: e.detail.value,
      ['typePick.isTypePick']: true
    })
  },
  numPickerChange: function (e) {
    this.setData({
      ['numPick.index']: e.detail.value,
      ['numPick.isNumPick']: true
    })
  },
  sexPickerChange: function (e) {
    this.setData({
      ['sexPick.index']: e.detail.value,
      ['sexPick.isSexPick']: true
    })
  },
  payPickerChange: function (e) {
    this.setData({
      ['payPick.index']: e.detail.value,
      ['payPick.isPayPick']: true
    })
  },
  travelPickerChange: function (e) {
    this.setData({
      ['travelPick.index']: e.detail.value,
      ['travelPick.isTravelPick']: true
    })
  },
  bindDateChange: function (e) {
    this.setData({
      eventData: e.detail.value,
    })
  },
  bindTimeChange: function (e) {
    if (e.target.id === 'eventStartTime') {
      this.setData({
        eventStartTime: e.detail.value,
      })
    } else {
      this.setData({
        eventEndTime: e.detail.value,
      })
    }
  },
  getTitle: function (e) {
    this.setData({
      eventTitle: e.detail.value
    })
  },
  getEventContent: function (e) {
    this.setData({
      eventContent: e.detail.value
    })
  },
  linkToPage() {
    app.globalData.isChangeRule = true;
    wx.navigateTo({
      url: '../restMap/restMap'
    })
  },
  compareTime(t1, t2) {
    var date = new Date();
    var a = t1.split(":");
    var b = t2.split(":");
    return date.setHours(a[0], a[1]) < date.setHours(b[0], b[1]);
  },
  compareData(date1, date2) {
    var oDate1 = new Date(date1);
    var oDate2 = new Date(date2);
    return oDate1.getTime() < oDate2.getTime()
  },
  MinutesTest(e) {
    e = e.replace(/-/g, '/');
    let sdate1 = new Date(e);
    sdate1.setMinutes(sdate1.getMinutes() - 1);
    let timeOne = sdate1.getHours();
    let timeTwo = sdate1.getMinutes();
    if (timeOne < 10) {
      timeOne = '0' + timeOne;
    }
    if (timeTwo < 10) {
      timeTwo = '0' + timeTwo;
    }
    let now = timeOne + ":" + timeTwo;
    return now;
  },
  // 得到发布活动的信息
  getPublishInfo: function () {
    let that = this;
    let eventInfo = this.data.eventInfo;
    //标题
    if (this.data.eventTitle) {
      eventInfo.eventTitle = this.data.eventTitle;
      if (this.data.eventTitle.length > 20) {
        app.showUserInfo('活动标题不能超过20字', 'none', 1500);
        return
      }
    } else {
      app.showUserInfo('请填写标题', 'none', 1500);
      return
    }
    //类型
    if (this.data.typePick.index === "0") {
      eventInfo.eventType = "find"
    } else if (this.data.typePick.index === "1") {
      eventInfo.eventType = 'partner'
    } else if (this.data.typePick.index === "2") {
      eventInfo.eventType = 'chat'
    } else {
      app.showUserInfo('请填写活动类型', 'none', 1500);
      return
    }
    //时间
    if (!(this.data.eventData)) {
      app.showUserInfo('请填写活动日期', 'none', 1500);
      return
    }
    if (!(this.data.eventStartTime)) {
      app.showUserInfo('请填写开始时间', 'none', 1500);
      return
    }
    if (!(this.data.eventEndTime)) {
      app.showUserInfo('请填写结束时间', 'none', 1500);
      return
    }
    eventInfo.eventStartTime = this.data.eventData + " " + this.data.eventStartTime + ':00';
    eventInfo.eventEndTime = this.data.eventData + " " + this.data.eventEndTime + ':00';
    eventInfo.registrationEndTime = this.data.eventData + " " + this.MinutesTest(eventInfo.eventStartTime) + ':59';

    //介绍
    if (this.data.eventContent) {
      eventInfo.eventContent = this.data.eventContent;
    } else {
      app.showUserInfo('请填写介绍', 'none', 1500);
      return
    }

    //人数
    if (this.data.numPick.array[this.data.numPick.index]) {
      eventInfo.eventNumber = this.data.numPick.array[this.data.numPick.index];
    } else {
      app.showUserInfo('请填写参与人数', 'none', 1500);
      return
    }

    //性别
    if (this.data.sexPick.index + "") {
      eventInfo.registrationSex = this.data.sexPick.index + "";
    } else {
      app.showUserInfo('请填写参与者性别', 'none', 1500);
      return
    }

    //付款方式
    if (this.data.payPick.index === "0") {
      eventInfo.payMethod = "me_treat"
    } else if (this.data.payPick.index === "1") {
      eventInfo.payMethod = 'aa'
    } else if (this.data.payPick.index === "2") {
      eventInfo.payMethod = 'male_a_female_free'
    } else {
      app.showUserInfo('请填写买单方式', 'none', 1500);
      return
    }

    //地址
    if (this.data.eventAddress != '请选择') {
      eventInfo.eventAddress = this.data.eventAddress;
    } else {
      app.showUserInfo('请填写地址', 'none', 1500);
      return
    }

    //出行方式
    if (this.data.travelPick.index === "0") {
      eventInfo.travelModel = 'self'
    } else if (this.data.travelPick.index === "1") {
      eventInfo.travelModel = 'me_transfer'
    } else {
      app.showUserInfo('请填写出行方式', 'none', 1500);
      return
    }
    eventInfo.longitude = this.data.longitude;
    eventInfo.latitude = this.data.latitude;
    eventInfo.cityCode = this.data.cityCode;
    this.setData({
      eventInfo: eventInfo
    })
    if (!this.compareTime(this.data.eventStartTime, this.data.eventEndTime)) {
      app.showUserInfo('需未来时间', 'none', 1500);
      return;
    }
    wx.showModal({
      title: '修改活动',
      content: '是否修改',
      success(res) {
        console.log(res)
        if (res.confirm) {
          that.saveEventInfo(eventInfo);
        }
      }
    })
  },
  // 发布活动
  saveEventInfo: function (params) {
    params.id = this.data.id;
    util.http({
      url: API.URL.EVENT.EDIT_EVENTINFO,
      data: {
        ...params
      },
      success: function (res) {
        console.log(res);
        wx.showToast({
          title: '修改成功',
          duration: 1500
        })
        setTimeout(function () {
          app.globalData.isNewMyFan = true;
          wx.navigateBack();
        }, 1500)
      },
      fail: function (res) {
        console.log(res);
        app.showUserInfo(res.msg, 'none', 2000);
      }
    })
  },
  getOldInfo: function (eventId) {
    util.http({
      url: API.URL.EVENT.FIND_EVENTINFODETAILBYID,
      data: {
        id: eventId
      },
      success: (res) => {
        if (!res.data.bussData) {
          app.showUserInfo('活动已取消，2s后自动返回', 'none', 2000)
          setTimeout(() => {
            wx.navigateBack({})
          }, 2000)
        }
        console.log('获取到的活动详情为：', res.data.bussData);
        this.setData({
          oldInfo: res.data.bussData
        })
        this.romanceInfo(res.data.bussData);
      },
      fail: (rej) => {
        wx.showToast({
          title: '获取不到活动信息',
          icon: "none",
          duration: 2000
        });
        console.log(rej);
      }
    });
  },
  romanceInfo: function(params){
    let eventInfo = {};
    let eventTypeIndex = 0;
    if (params.eventType === "find") {
      eventTypeIndex = '0';
    } else if (params.eventType === "partner") {
      eventTypeIndex = '1';
    } else{
      eventTypeIndex = '2';
    }
    let sexIndex = 0;
    if (params.registrationSex === '女'){
      sexIndex = '2';
    } else if (params.registrationSex === '男'){
      sexIndex = '1';      
    } else{
      sexIndex = '0';
    }
    let  payIndex = 0;
    if (params.payMethod === '我请客') {
      payIndex = '0';
    } else if (params.payMethod === '男A女免') {
      payIndex = '2';
    } else {
      payIndex = '1';
    }

    let travelIndex = 0;
    if (params.travelModel === '各自出行'){
      travelIndex = '0';
    } else {
      travelIndex = '1';      
    }
    this.setData({
      eventTitle: params.eventTitle,
      eventContent: params.eventContent,
      ['typePick.index']: eventTypeIndex,
      registrationData: params.registrationEndTime.split(" ")[0],
      eventData: params.eventStartTime.split(" ")[0],
      eventStartTime: params.eventStartTime.split(" ")[1].substring(0, params.eventStartTime.split(" ")[1].length - 3),
      eventEndTime: params.eventEndTime.split(" ")[1].substring(0, params.eventEndTime.split(" ")[1].length - 3),
      ['numPick.index']: params.eventNumber - 1,
      ['sexPick.index']: sexIndex,
      ['payPick.index']: payIndex,
      ['travelPick.index']: travelIndex,
      eventAddress: params.eventAddress,
      longitude: params.longitude,
      latitude: params.latitude
    })
  },
  onLoad: function (options) {
    this.setData({
      id: options.id,
      cityCode: options.cityCode
    })
    this.getOldInfo(options.id)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    let globalData = app.globalData;
    if (globalData.title) {
      this.setData({
        eventAddress: globalData.title,
        longitude: globalData.longitude,
        latitude: globalData.latitude,
        cityCode: globalData.cityCode
      })
      app.globalData.title = '';
      app.globalData.longitude = '';
      app.globalData.latitude = '';
      app.globalData.cityCode = '';
    }
    else {
      this.setData({
        eventAddress: "请选择"
      })
    }
  }
})